import vue from '@vitejs/plugin-vue'
import path from 'path';
import { resolve } from 'path';
import { loadEnv } from 'vite'

const root = process.cwd();

function pathResolve(dir: string) {
    return resolve(__dirname, '.', dir);
}

// https://vitejs.dev/config/
export default ({ command, mode }) => {
    const viteEnv = loadEnv(mode, root);
    const { VITE_PORT, VITE_PUBLIC_PATH, VITE_DROP_CONSOLE, VITE_LEGACY } = viteEnv;

    const isBuild = command === 'build';

    return {
        base: VITE_PUBLIC_PATH,
        root,
        resolve: {
            alias: [
                {
                    find: /^\/@\//,
                    replacement: pathResolve('src') + '/',
                },
            ]
        },
        server: {
            port: Number(VITE_PORT),
            hmr: {
                overlay: true,
            },
            cors: true,
            open: true
        },
        build: {
            target: 'modules', // modules
            outDir: 'dist',
            assetsDir: 'static',
            sourcemap: false,
            polyfillDynamicImport: Boolean(VITE_LEGACY),
            terserOptions: {
                compress: {
                    keep_infinity: true,
                    drop_console: Boolean(VITE_DROP_CONSOLE),
                },
            },
        },
        plugins: [
            vue(),
        ],
    }
}